package com.iweb.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iweb.blog.dao.mapper.ArticleMapper;
import com.iweb.blog.dao.pojo.Article;
import com.iweb.blog.service.ArticleService;
import com.iweb.blog.service.SysUserService;
import com.iweb.blog.service.TagService;
import com.iweb.blog.vo.ArticleVo;
import com.iweb.blog.vo.Result;
import com.iweb.blog.vo.TagVo;
import com.iweb.blog.vo.param.PageParams;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {
@Autowired
    private ArticleMapper articleMapper;
@Autowired
private SysUserService sysUserService;
@Autowired
private TagService tagService;
    @Override
    public List<ArticleVo> listArticlesPage(PageParams pageParams) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        Page<Article> page = new Page<>(pageParams.getPage(),pageParams.getPageSize());
        Page<Article> articlePage = articleMapper.selectPage(page, queryWrapper);
        List<ArticleVo> articleVoList = copyList(articlePage.getRecords(),true,false,true);
        return articleVoList;
    }

@Override
public Result hotArticles(int limit){
    LambdaQueryWrapper<Article>queryWrapper=new LambdaQueryWrapper<>();
    queryWrapper.orderByDesc(Article::getViewCounts);
    queryWrapper.select(Article::getId,Article::getTitle);
    queryWrapper.last(" limit "+limit);
    List<Article>articles=articleMapper.selectList(queryWrapper);
    return Result.success(copyList(articles,false,false,false));
}

    @Override
    public Result newArticles(int limit) {
        LambdaQueryWrapper<Article>wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Article::getCreateDate);
        wrapper.select(Article::getId,Article::getTitle);
        wrapper.last(" limit "+limit);
        List<Article>articles=articleMapper.selectList(wrapper);
        return Result.success(copyList(articles,false,false,false));
    }

    @Override
    public List<ArticleVo> listArchives() {
    return articleMapper.listArchives();
    }

    @Override
    public Result findArticleById(Long id) {
        Article article = articleMapper.selectById(id);

        ArticleVo articleVo= copy(article,true,true,true);
        return Result.success(articleVo);
    }

    private List<ArticleVo> copyList(List<Article> records,boolean isAuthor,boolean isBody,boolean isTags) {
        List<ArticleVo> articleVoList = new ArrayList<>();
        for (Article article : records) {
            ArticleVo articleVo = copy(article,isAuthor,isBody,isTags);
            articleVoList.add(articleVo);
        }
        return articleVoList;
    }

    private  ArticleVo copy(Article article,boolean isAuthor,boolean isBody,boolean isTags){
    ArticleVo articleVo=new ArticleVo();
        BeanUtils.copyProperties(article,articleVo);
        articleVo.setCreateDate(new DateTime(article.getCreateDate()).toString("yyyy-MM-dd HH:mm"));
        String nickName=sysUserService.findNickNameByAuthorId(article.getAuthorId());
        articleVo.setAuthor(nickName);
        List<TagVo> tagVos=tagService.findTagById(article.getId());
        articleVo.setTags(tagVos);
        return articleVo;
    }
}
